package util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


public class AESUtil {
	
	public static String encrypt(String content, byte[] aeskey) {
		try {
			Cipher aesECB = Cipher.getInstance("AES/ECB/PKCS5Padding");
			SecretKeySpec key = new SecretKeySpec(aeskey, "AES");
			aesECB.init(Cipher.ENCRYPT_MODE, key);
			byte[] result = aesECB.doFinal(content.getBytes("utf-8"));
			return Base64Util.encode(result);

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String decrypt(String content, byte[] aeskey) {
		try {
		    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器
           SecretKeySpec key = new SecretKeySpec(aeskey, "AES");
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
			
			byte[] result = Base64Util.decode(content);
			
			return new String(cipher.doFinal(result),"utf-8"); // 解密
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
